<?php
// 本类由系统自动生成，仅供测试用途
namespace Home\Controller;
use Think\Controller;

class IndexController extends Controller {
    public function index(){
    	//获取轮播图片
    	$this->flashlist = M('Flash')
    					->field('brief, url, image')
    					->where(array('visible' => 'y'))
						->order('sequence asc')
						->select();
		
		//获取服务解决方案
		$this->service = M('Service')
						->field('title, description, content, icon')
						->where(array('visible' => 'y'))
						->order('sequence asc')
						->select();
						
		//获取案例分类
		$this->casetype = D('CasetypeView')
						->order('ct.sequence asc')
						->group('c.type')
						->select();
						
		//获取案例列表
		$this->case = M('Case')
						->field('id, title, image, type')
						->where(array('visible' => 'y'))
						->order('sequence asc')
						->select();
			
						
		//获取友情链接列表
		$this->linklist = M('Link')
						->field('name, url')
						->where(array('visible' => 'y'))
						->order('sequence asc')
						->select();
    	
		$this->display();
	}
	
	/**
	 * 案例详情
	 */
	 public function cdetail(){
	 	$cid = $_GET['id'];
		$this->case = M('Case')
						->field('title, abstract, content')
						->where(array('id' => $cid))
						->find();
		
		$this->display();
	 }
	
	/**
	 * 留言
	 */
	 public function sendmessage(){
	 	if($_POST['name'] == ''){
			$this->error("请您留言，谢谢！");
			exit();
		}
	 	if($_POST['email'] == '' && $_POST['QQ'] == ''){
			$this->error("邮箱与QQ号请至少填写一个，方便我们联系您！");
			exit();
		}
	 	if($_POST['email'] != ''){
			$pattern = '/\w+@(\w|\d)+\.\w{2,3}/i';
			preg_match($pattern, $_POST['email'], $matches);
			if(count($matches) == 0){
				$this->error("邮箱地址格式不正确，请重新填写！");
				exit();
			}
		}
	 	if(!is_numeric($_POST['QQ']) && $_POST['QQ'] != ''){
			$this->error("QQ号必须为数字，请重新填写！");
			exit();
		}
	 	if($_POST['message'] == ''){
			$this->error("请您留言，谢谢！");
			exit();
		}
		
		$data = array(
			'name'		=> I('name'),
			'email'		=> I('email'),
			'qq'		=> I('QQ'),
			'content'	=> I('message'),
			'uid'		=> 0,
			'time'		=> time(),
			'reply'		=> 'n'
		);
		
		if(M('Feedback')->add($data)){
			$this->success('留言成功，感谢您的关注！');
			exit;
		}
	 }
}